<?php

namespace app\admin\controller;

use app\admin\logic\Depar as Depar_logic;
use nx\Data;
use think\Db;
use think\Cache;

class Depar extends Admin
{

    /**
     * 部门列表
     * @return mixed
     */
    public function index()
    {
        $depar_logic = new Depar_logic();
        $depars = $depar_logic->getAllDeparToTree();
        $qudaodepar_id = collect(Data::channelList($depars, 1, '— ', 'id', 'pid'))->pluck('id')->all();
        $this->assign('qudaodepar_id', $qudaodepar_id);
        $this->assign('depars', $depars);
        return $this->fetch();
    }

    /**
     * 添加部门
     * @return mixed
     */
    public function add()
    {
        $depar_logic = new Depar_logic();
        if ($this->request->isPost()) {
            $data = input('post.');
            $arr = session('admin_user');
            logdaily(9,$arr['username'],'新增部门：'.$data['name']);
            $result = $depar_logic->addDepar($data);
            if (false === $result['result']) {
                $this->error($result['msg']);
            } else {
                $this->success($result['msg']);
            }
        } else {
            $pid = $this->request->param('id');
            !empty($pid) ?: $pid = 0;
            $companys = $depar_logic->getAllDeparToTree();
            $this->assign('pid', $pid);
            $this->assign('companys', $companys);
            return $this->fetch();
        }
    }


    /**
     * 编辑部门
     * @return mixed
     */
    public function edit()
    {
        $depar_logic = new Depar_logic();
        if ($this->request->isPost()) {
            $data = input('post.');
            $result = $depar_logic->editDepar($data);
            $arr = session('admin_user');
            logdaily(10,$arr['username'],'修改部门：'.$data['name']);
            if ($result['result'] === true) {
                $this->success($result['msg']);
            } else {
                $this->error($result['msg']);
            }
        } else {
            $id = input('id');
            $depar = Db::name('depar')->where('id', $id)->find();
            $depars = $depar_logic->getAllDeparToTree();
            $this->assign('depars', $depars);
            $this->assign('info', $depar);
            return $this->fetch();
        }
    }

    /**
     * 删除部门
     */
    public function del()
    {
        $id = $this->request->param('id', 0);
        $data = Db::name('depar')->where('id',$id)->find();
        $arr = session('admin_user');
        logdaily(11,$arr['username'],'删除部门：'.$data['name']);
        $depar_logic = new Depar_logic();
        $result = $depar_logic->delDepar($id);
        if ($result['result'] === true) {
            $this->success($result['msg']);
        } else {
            $this->error($result['msg']);
        }
    }


    /**
     * 设置部门启用和禁用
     * @return \think\response\Json
     */
    public function setStatus(){
        $id = input('id');
        if(empty($id)){
            $this->error('ID丢失');
        }
        $info = Db::name('depar')->where('id',$id)->field('id,status')->find();
        $status = [1,0];
        $data['id'] = $id;
        $data['status'] = $status[$info['status']];
        $res = Db::name('depar')->update($data);
        if($res){
            Cache::rm('allDepars');
            $this->success('设置成功');
        }else{
            $this->error('设置失败');
        }
    }

    /**
     * 设置部门默认基础角色
     * @return mixed
     * @author: Huang
     */
//    public function setstates()
//    {
//        if ($this->request->isPost()) {
//            dump(1111);
//            $authdata = $this->request->post();
//            if (empty($authdata['id'])) {
//                $this->error('参数丢失');
//            } else {
//                $data['id'] = $authdata['id'];
//                $data['rule'] = implode(',', $this->request->post('rule/a'));
//                $res = model('depar')->allowField(true)->isUpdate()->save($data);
//                if ($res) {
//                    $this->success('操作成功');
//                } else {
//                    $this->error(model('depar')->getError());
//                }
//            }
//        } else {
//            dump(222);
//            $deparid = input('id', 0, 'int');
//            $depar = Db::name('depar')->where(['id' => $deparid])->find();//当前部门
//            $auth_group = Db::name('auth_group')->cache(6000)->select();//获取所有的角色
//            $this->assign('depar', $depar);
//            $this->assign('auth_group', $auth_group);
//            return $this->fetch();
//        }
//    }

    /**
     * 设置部门绑定渠道
     * @return mixed
     * @author: Huang
     */
    public function setstates()
{
    if ($this->request->isPost()) {
        $authdata = $this->request->post();
        if (empty($authdata['id'])) {
            $this->error('参数丢失');
        } else {
            $data['id'] = $authdata['id'];
            $data['rule'] = implode(',', $this->request->post('rule/a'));
            $res = model('depar')->allowField(true)->isUpdate()->save($data);
            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error(model('depar')->getError());
            }
        }
    } else {
        //获取部门
        $qudao_Logic = new \app\admin\logic\Qudao();//渠道
        $depar_Logic = new \app\admin\logic\Depar();//部门
        $deparid = input('id', 0, 'int');
        $pid = Db::name('depar')->where(['id' => $deparid])->find();
        $where['pid'] = $pid['pid'];
        $ly_ids = array_unique(collect($depar_Logic->getModel()->where($where)->column('rule'))->flatMap(function ($item) {
            return explode(',', $item);
        })->all());//获取所有渠道已经被分配了的分组的id

        //获取所有正常的渠道id 判断是不是渠道部门下的人员 不是就取部门分配的渠道
        if ($pid['pid'] == 1) {
            $leibie_id = $qudao_Logic->getModel()->where(['status' => 1])->column('id');
        } else {
            $leibie_id = array_unique(collect($depar_Logic->getModel()->where('id', $pid['pid'])->column('rule'))->flatMap(function ($item) {
                return explode(',', $item);
            })->all());
        }
        $lyfz_diff = array_diff($leibie_id, $ly_ids);//排除没有被分配过的渠道id

        $lyfz = $depar_Logic->getModel()->where(['id' => $deparid])->find();//获取当前组情况所拥有的来源id
        $map = ['status' => 1];
        $map['id'] = ['in', implode(',', array_merge(empty($lyfz['rule']) ? [] : explode(',', $lyfz['rule']), $lyfz_diff))];
        $data = $qudao_Logic->getModel()->where($map)->select();
        $this->assign('ly_ids', $ly_ids);
        $this->assign('info', $data);
        $this->assign('user_groups', $lyfz['rule']);
        $this->assign('lyfz', $lyfz);




//        $depar_Logic = new \app\admin\logic\Depar();//部门
//        $deparid = input('id', 0, 'int');
//        $depar = Db::name('depar')->where(['id' => $deparid])->find();//当前部门
//
//        //绑定线上渠道
//
//        $qudao_Logic = new \app\admin\logic\Qudao();//渠道
//        $arr = $qudao_Logic->getAllText();
//        dump($depar['pid']);
//        foreach ($arr as $k=>$v){
//            $leibie_id[$k] = $v['id'];//获取所有正常的渠道id
//        }
//        $ly_ids = array_unique(collect($depar_Logic->getModel()->column('rule'))->flatMap(function ($item) {
//            return explode(',', $item);
//        })->all());//获取所有渠道已经被分配了的分组的id
//        dump($ly_ids);
//        //获取所有正常的渠道id 判断是不是渠道部门下的人员 不是就取部门分配的渠道
//        if ($depar['pid'] == 1) {
//            $leibie_id = $qudao_Logic->getModel()->where(['status' => 1])->column('id');
//        } else {
//            $leibie_id = array_unique(collect($depar->getModel()->where('id', $depar['pid'])->column('rule'))->flatMap(function ($item) {
//                return explode(',', $item);
//            })->all());
//        }
//        $lyfz_diff = array_diff($leibie_id, $ly_ids);//排除没有被分配过的渠道id
//        $lyfz = $depar_Logic->getModel()->where(['id' => $deparid])->find();//获取当前组情况所拥有的来源id
//        $map = ['status' => 1];
//        $map['id'] = ['in', implode(',', array_merge(empty($lyfz['rule']) ? [] : explode(',', $lyfz['rule']), $lyfz_diff))];
//        $data = $qudao_Logic->getModel()->where($map)->select();

        //绑定线下渠道
//        $info = $qudao_Logic->getAllTexts();

//        $this->assign('xsqudao', $arr);
//        $this->assign('lyfz', $lyfz);
//        $this->assign('data', $data);
//        $this->assign('depar', $depar);
//        $this->assign('info', $info);



        return $this->fetch();
    }
}

}
